from django.db import models

from Contest.models import Contest
from Person.models import Person
from Problems.models import Problem, ProblemTag
from Submission.config import Verdict


# Create your models here.

class Submission(models.Model):
    user = models.ForeignKey(Person, on_delete=models.CASCADE, related_name='user_submissions')  # 提交人
    problem = models.ForeignKey(Problem, on_delete=models.CASCADE, related_name='problem_submissions')
    contest = models.ForeignKey(Contest, on_delete=models.CASCADE, related_name='contest_submissions', null=True,blank=True)

    code = models.TextField()

    verdict = models.CharField(max_length=10, default=Verdict.PENDING, choices=Verdict.VERDICT_CHOICES)
    remote_verdict = models.CharField(max_length=100, default=None, null=True, blank=True)
    lang = models.CharField(max_length=20, null=False, blank=False)
    remote_id = models.CharField(max_length=100, null=True, blank=True, verbose_name="远程评测ID")

    submission_time = models.DateTimeField(auto_now_add=True, null=True, blank=True)
    time_cost = models.CharField(max_length=10, null=True, blank=True)
    memory_cost = models.CharField(max_length=20, null=True, blank=True)

    recognized = models.ManyToManyField(ProblemTag, related_name='recognized_submissions', blank=True)

    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间')
    is_delete = models.BooleanField(default=False, verbose_name='删除标记')

    # 程序编译运行的时候返回的一些错误信息和编译失败的错误信息
    additional_info = models.TextField(default=None, null=True, blank=True)

    is_public = models.BooleanField(default=True)

    hash_val = models.CharField(max_length=100, null=True, blank=True)

    def __str__(self):
        return f'提交人:{self.user} 问题ID:{self.problem} 结果:{self.verdict},远程ID:{self.remote_id}'

    class Meta:
        ordering = ["-create_time"]
